package ebj.common.annotation.demo_data_provider_manager.helper;

import com.google.common.collect.Maps;
import com.google.common.collect.Ordering;

import ebj.common.annotation.demo_data_provider_manager.anno.AnnoProcessor;
import ebj.common.annotation.demo_data_provider_manager.anno.QueryParameter;
import ebj.common.annotation.demo_data_provider_manager.data_provider.DataProvider;

import org.apache.commons.io.output.StringBuilderWriter;
//import org.apache.commons.io.output.StringBuilderWriter;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.reflections.ReflectionUtils;

import java.lang.reflect.Field;
import java.util.*;

/**
 * Created by yfyuan on 2016/8/15.
 */
public class WebViewHelper {

    private static VelocityEngine velocityEngine;

    static {
        Properties props = new Properties();
        props.setProperty("resource.loader", "class");
        props.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        velocityEngine = new VelocityEngine(props);
    }

    private static Map<String, String> rendered = Maps.newHashMap();

    public static String getQueryView(DataProvider dataProvider) throws InstantiationException, IllegalAccessException {
        List<Map<String, String>> alltParammeters = AnnoProcessor.geAllQueryParammeters(dataProvider);
        VelocityContext context = new VelocityContext();
        context.put("params", alltParammeters);
        StringBuilderWriter stringBuilderWriter = new StringBuilderWriter();
        velocityEngine.mergeTemplate("template/config/query.vm", "utf-8", context, stringBuilderWriter);
        return stringBuilderWriter.toString();
    }

    public static String getDatasourceView(DataProvider dataProvider) throws InstantiationException, IllegalAccessException {
        VelocityContext context = new VelocityContext();
        List<Map<String, String>> allDataProviderAttributes = AnnoProcessor.geAllDataProviderAttributes(dataProvider);
        context.put("params", allDataProviderAttributes);
        StringBuilderWriter stringBuilderWriter = new StringBuilderWriter();
        velocityEngine.mergeTemplate("template/config/datasource.vm", "utf-8", context, stringBuilderWriter);
        return stringBuilderWriter.toString();
    }
}